<?php

use Illuminate\Database\Seeder;
use App\Model\Admin\Article;
use App\Model\Admin\Tag;
use Faker\Factory;

class ArticleTagTableSeeder extends Seeder
{
    /**
     * php artisan make:seeder ArticleTagTableSeeder
     *
     * php artisan db:seed --class=ArticleTagTableSeeder
     *
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::table('article_tag')->truncate();
        $articles = Article::all();
        $tags = Tag::all()->pluck('id')->toArray();
        $faker = Factory::create();

        foreach ($articles as $article) {
            $data = [];
            shuffle($tags);

            $start = rand(0, count($tags) - 3);
            $subTags = array_slice($tags, $start, $start);

            if (count($subTags) < 0 )continue;

            foreach ($subTags as $key => $tag) {
                $startDate = $faker->dateTimeBetween();
                $data[] = [
                    'tag_id' => $tag,
                    'article_id' => $article->id,
                    'is_active' => rand(0, 1),
                    'created_at' => $startDate,
                    'updated_at' => $faker->dateTimeBetween($startDate),
                ];
            }

            $r = DB::table('article_tag')->insert($data);
        }
    }
}
